<template>
  <el-card class="card">
    <!-- 搜索 -->
    <el-row type="flex" justify="space-around">
      <el-col :span="12">
        <el-form ref="serchFrom">
          <el-form-item label="关键字">
            <el-input
              v-model="serchkeys"
              clearable
              style="width: 300px"
              @keyup.native.enter="serchRandoms"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-col>

      <el-col :span="12">
        <el-row type="flex" justify="end">
          <el-button @click="delSerch">{{
            delOrReture ? "清除" : "返回"
          }}</el-button>
          <el-button @click="serchRandoms" type="primary">搜索</el-button>
        </el-row>
      </el-col>
    </el-row>

    <!-- 警示框 -->
    <el-alert type="info" show-icon class="alert-tip">
      <div slot="title">数据一共有{{ total }}条</div>
    </el-alert>

    <!-- table表格 -->
    <el-table :data="randomsData" style="width: 100%" align="center">
      <el-table-column
        prop="id"
        label="编号"
        width="200"
        align="center"
      ></el-table-column>

      <!-- 题型 -->
      <el-table-column
        prop="questionType"
        label="题型"
        width="180"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="questionIDs"
        width="200"
        label="题目编码"
        align="center"
      >
        <template slot-scope="{ row }">
          <div
            v-for="(item, index) in row.questionIDs"
            :key="index"
            class="title-code"
            @click="seeCode(item.id)"
          >
            {{ item.number }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="addTime"
        label="录入时间"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="totalSeconds"
        label="答题时间(s)"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="accuracyRate"
        label="正确率(%)"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="userName"
        label="录入人"
        align="center"
      ></el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="{ row }">
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            circle
            @click="delRandoms(row.id)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-row type="flex" justify="end" class="paging">
      <el-pagination
        background
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :current-page.sync="currentPage"
        @current-change="changeCurrent"
        @size-change="changeSize"
        :page-sizes="[1, 2, 5, 10, 20]"
        :page-size="20"
      ></el-pagination>
    </el-row>

    <RandomsPreview
      ref="RandomsPreviewRef"
      :isShow.sync="isShow"
      v-if="isShow"
    ></RandomsPreview>
  </el-card>
</template>

<script>
import { getRandomsList, delRandomsById } from '@/api/hmmm/randoms.js'
import { questionType } from '@/api/hmmm/constants.js'
import RandomsPreview from '../components/randoms-preview.vue'
export default {
  components: {
    RandomsPreview
  },
  data () {
    return {
      randomsData: [], // 组题列表
      serchkeys: '', // 搜索参数
      // 表格配置参数
      page: {
        page: 1,
        pagesize: 20,
        keyword: ''
      },
      // 列表总数
      total: 0,
      isShow: false,
      delOrReture: true,
      currentPage: 1
    }
  },
  methods: {
    async  delSerch () {
      this.serchkeys = ''
      const { data: { items, counts } } = await getRandomsList(this.$options.data().page)

      for (const val of items) {
        const item = questionType.find(item => item.value === parseInt(val.questionType))
        val.questionType = item.label
      }
      this.randomsData = items
      this.total = counts
      this.delOrReture = true
      this.page = this.$options.data().page
    },
    // 获取组题列表
    async getRandomsList () {
      const { data: { items, counts } } = await getRandomsList(this.page)

      for (const val of items) {
        const item = questionType.find(item => item.value === parseInt(val.questionType))
        val.questionType = item.label
      }
      this.randomsData = items
      this.total = counts
      // console.log(counts)
      // console.log(items)
    },
    // 改变页码
    changeCurrent (val) {
      // 修改页码
      this.page.page = val
      // 重新获取页面
      this.getRandomsList()
    },
    // 改变页容量
    changeSize (val, a) {
      this.page.pagesize = val
      this.getRandomsList()
      this.currentPage = 1
    },
    // 根据关键字搜索
    serchRandoms () {
      if (this.serchkeys.length === 0) {
        return this.$message({
          message: '关键字不能为空',
          type: 'warning',
          showClose: true,
          duration: 3000
        })
      }
      this.page.page = 1
      this.page.keyword = this.serchkeys
      // 重新获取列表
      this.getRandomsList()
      this.delOrReture = false
      // // 清空表单
      // this.serchkeys = ''
    },
    // 删除组题
    delRandoms (id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        const { data } = await delRandomsById({ id })
        console.log(data)
        this.$message({
          message: '删除成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
        this.getRandomsList()
      }).catch((err) => {
        console.log(err)
      })
    },
    // 预览题目
    seeCode (id) {
      this.isShow = true
      this.$nextTick(async () => {
        await this.$refs.RandomsPreviewRef.seeCodeChild(id)
      })
    }
  },
  created () {
    this.getRandomsList()
  }
}
</script>

<style scoped lang='scss'>
.card {
  padding: 0 20px;
  // height: 100vh;
  margin-bottom: 20px;
  // background-color: rgba(0, 0, 0, 0.01);
}
.alert-tip {
  margin-bottom: 15px;
}
.paging {
  margin-top: 20px;
}
.title-code {
  color: #409eff;
  cursor: pointer;
}
// ::v-deep .el-form-item {
//   width: 25%;
// }
</style>
